package entities 
{
	import org.flixel.FlxG;
	/**
	 * ...
	 * @author morgan
	 */
	public class PushBlock extends ReflectionSprite
	{
		[Embed (source = "../../data/pushBlock.png")] private var img_block:Class
		public var targetX:Number = 0;
		public var failTimer:Number = 0;
		public function PushBlock(x:int, y:int, scene:GameScene) 
		{
			super(x, y, scene, img_block);
			immovable = true;
			targetX = x;
		}
		
		override public function update():void 
		{
			super.update();
			if (x < targetX)
			{
				velocity.x = 70
			}
			else if (x > targetX)
			{
				velocity.x = -70;
			}
			if (Math.abs(x - targetX) < 100 * FlxG.elapsed)
			{
				x = targetX;
				velocity.x = 0;
				failTimer = 0;
			}
			if (failTimer <= 0)
			{
				targetX = x;
			}
			failTimer -= FlxG.elapsed;
		}
		
		override public function collides(o:ReflectionSprite):void 
		{
			super.collides(o);
			if (o is Player && o.y > y - 4)
			{
				var p:Player = o as Player;
				p.pushTimer += FlxG.elapsed;
				if (p.pushTimer > 0.25)	// It actually takes twice this long because the player constantly decrements the timer
				{
					p.pushTimer = 0;					
					if (o.x < x && !scene.tilemap.getTile(x / 16 + 1, y / 16))
					{
						targetX = x + 16;
						failTimer = 0.5;
					}
					if (o.x > x && !scene.tilemap.getTile(x / 16 - 1, y / 16))
					{
						targetX = x - 16;
						failTimer = 0.5;
					}
				}
			}
		}
	}

}